package com.letoken.platform.pub.req;

import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * PageRes
 *
 */
@Data
@ApiModel(value = "分页信息对象", description = "分页信息对象")
@AllArgsConstructor
public class PageRes implements Serializable {

    private int size;
    private int page;
    private long total;
    private int pageCount;

    public PageRes() {
    }

    public PageRes(int page, int size, long total) {
        this.page = page;
        this.size = size;
        this.total = total;
        this.pageCount = getPageCount();
    }

    public int getPageCount() {
        return this.getTotal() == 0 ? 1 : (int) Math.ceil((double) this.total / (double) this.getSize());
    }

    /**
     * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
     */
    public int getFirst() {
        return (page - 1) * size + 1;
    }

    /**
     * 是否还有下一页.
     */
    public boolean isHasNext() {
        return page < pageCount;
    }

    /**
     * 取得下页的页号, 序号从1开始. 当前页为尾页时仍返回尾页序号.
     */
    public int getNextPage() {
        if (isHasNext()) {
            return page + 1;
        } else {
            return page;
        }
    }

    /**
     * 是否还有上一页.
     */
    public boolean isHasPre() {
        return page > 1;
    }

    /**
     * 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.
     */
    public int getPrePage() {
        if (isHasPre()) {
            return page - 1;
        } else {
            return page;
        }
    }

}